using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;


using System.Data.Linq;
using System.Linq;

using DataLayer;

namespace BusinessLayer.Activities
{
   public partial class Reorder_TabWidgets : System.Workflow.ComponentModel.Activity
   {
      public Reorder_TabWidgets()
      {
         //InitializeComponent();
      }

      private static DependencyProperty TabIdProperty = DependencyProperty.Register("TabId", typeof(int), typeof(Reorder_TabWidgets));
      public int TabId
      {
         get { return (int)base.GetValue(TabIdProperty); }
         set { base.SetValue(TabIdProperty, value); }
      }

      private static DependencyProperty ColumnNoProperty = DependencyProperty.Register("ColumnNo", typeof(int), typeof(Reorder_TabWidgets));
      public int ColumnNo
      {
         get { return (int)base.GetValue(ColumnNoProperty); }
         set { base.SetValue(ColumnNoProperty, value); }
      }

      private static DependencyProperty OrderStrProperty = DependencyProperty.Register("OrderStr", typeof(string), typeof(Reorder_TabWidgets));
      public string OrderStr
      {
         get { return (string)base.GetValue(OrderStrProperty); }
         set { base.SetValue(OrderStrProperty, value); }
      }
      
      protected override ActivityExecutionStatus Execute(ActivityExecutionContext executionContext)
      {
         ArrayList WidgetOrder = new ArrayList();
         foreach (string s in OrderStr.Split(",".ToCharArray()))
         {
            if (s.Length > 0)
            {
               try
               {
                  WidgetOrder.Add(Convert.ToInt32(s));
               }
               catch
               {
               }
            }
         }

         ArrayList Widgets = new ArrayList();

         using (var db = DatabaseHelper.GetDashboardData())
         {
            //get all widgets inside this tab
            var ColumnWidgets = from p in db.TabWidgets
                                where p.TabId == TabId
                                orderby p.OrderNo
                                select p;

            //get all the widgets in the column and find their new order
            foreach (var w in ColumnWidgets)
            {
               int idx = WidgetOrder.IndexOf(w.Id);

               if (idx > -1)
               {
                  WidgetOrder[idx] = -(int)WidgetOrder[idx];
                  Widgets.Add(w);
               }
               else if (w.ColumnNo == ColumnNo)
               {
                  //add to order list only if column matches the column currently being adjusted
                  WidgetOrder.Add(-w.Id);
                  Widgets.Add(w);
               }
               //Add the widgets to an arraylist
            }

            //loop thru the widgetorder and remove those +ve ones (not found)
            for (int i = 0; i < WidgetOrder.Count; i++)
            {
               if (((int)WidgetOrder[i]) > -1)
               {
                  WidgetOrder.RemoveAt(i--);
               }
               else
               {
                  //put them back as positive for search
                  WidgetOrder[i] = -(int)WidgetOrder[i];
               }
            }

            //loop thru all the widgets and save their positions
            foreach (TabWidget obj in Widgets)
            {
               obj.OrderNo = WidgetOrder.IndexOf(obj.Id) + 1;
               obj.ColumnNo = ColumnNo;
            }

            db.SubmitChanges();
         }

         return ActivityExecutionStatus.Closed;
      }
   }
}